<?php


namespace app\admin\controller;

use app\model\web\Banner as BannerModel;


/**
 * 轮播管理
 */
class Banner extends BaseAdmin
{
    /**
     * 轮播列表
     */
    public function lists()
    {
        $banner = new BannerModel();
        if (request()->isAjax()) {
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $search_text = input('search_text', '');
            $is_show = input('is_show','');
            
            $condition = [

            ];

            if ($is_show!=''){
                $condition[] = ['a.is_show','=',$is_show];
            }
            if (!empty($search_text)) {
                $condition[] = [ 'a.banner_title', 'like', '%' . $search_text . '%' ];
            }
      
            $list = $banner->getBannerPageList($condition, $page, $page_size);

            return $list;
        } else {
            $this->forthMenu();

            return $this->fetch("banner/lists");
        }
    }

    /**
     * 添加轮播
     */
    public function addBanner()
    {
        $banner = new BannerModel();
        if (request()->isAjax()) {
            $data = [
                'type' => input('type', 0),
                'banner_title' => input('banner_title', ''),
                'banner_url' => input('banner_url', ''),
                'image' => input('image', ''),
                'slide_sort' => input('slide_sort', 0),
                'is_show' => input('is_show', 0),
                'create_time'=>time(),
                'uid'=>$this->user_info['uid'],
                'username'=>$this->user_info['username'],
            ];
            return $banner->addBanner($data);
        } else {
            //dump($this->user_info);die;
            return $this->fetch("banner/add_banner");
        }
    }

    /**
     * 编辑广告
     */
    public function editBanner()
    {
        $banner_id = input('banner_id', '');
        $banner = new BannerModel();
        if (request()->isAjax()) {
            $data = [
                'type' => input('type', 0),
                'banner_title' => input('banner_title', ''),
                'banner_url' => input('banner_url', ''),
                'image' => input('image', ''),
                'slide_sort' => input('slide_sort', 0),
                'is_show' => input('is_show', 0),
                'modify_time'=>time()
            ];
            return $banner->editBanner($data, [ [ 'banner_id', '=', $banner_id ] ]);
        } else {
            $banner_info = $banner->getBannerInfo($banner_id);
            $this->assign('banner_info', $banner_info[ 'data' ]);

            return $this->fetch("banner/edit_banner");
        }
    }

    /**
     * 修改广告字段
     */
    public function editBannerField()
    {
        if (request()->isAjax()) {
            $banner = new BannerModel();
            $type = input('type', '');
            $value = input('value', '');
            $banner_id = input('banner_id', '');
            $data = [
                $type => $value
            ];
            return $banner->editBanner($data, [ [ 'banner_id', '=', $banner_id ] ]);
        }
    }

    /**
     * 删除广告
     */
    public function deleteBanner()
    {
        if (request()->isAjax()) {
            $banner_ids = input('banner_ids', 0);
            $banner = new BannerModel();
            return $banner->deleteBanner([ [ 'banner_id', 'in', $banner_ids ] ]);
        }
    }

    public function editBannerStatus()
    {
        if (request()->isAjax()) {
            $banner_id = input('banner_id', 0);
            $is_show = input('is_show', 0);
            $banner = new BannerModel();
            return $banner->editBanner(['is_show'=>$is_show], [ [ 'banner_id', '=', $banner_id ] ]);
        }
    }

}